package cn.xny.txcc.service.impl;

import cn.xny.txcc.domain.*;

import cn.xny.txcc.dto.UserDto;
import cn.xny.txcc.exception.BusinessException;
import cn.xny.txcc.feignclients.UaaFeignClient;
import cn.xny.txcc.mapper.UserAccountMapper;
import cn.xny.txcc.mapper.UserBaseInfoMapper;
import cn.xny.txcc.mapper.UserCodeMapper;
import cn.xny.txcc.mapper.UserMapper;

import cn.xny.txcc.result.JSONResult;
import cn.xny.txcc.service.IUserService;

import cn.xny.txcc.util.BitStatesUtils;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.commons.lang3.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 会员登录账号 服务实现类
 * </p>
 *
 * @author whale
 * @since 2023-03-08
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;
    @Autowired
    private UaaFeignClient uaaFeignClient;
    @Autowired
    private UserAccountMapper userAccountMapper;
    @Autowired
    private UserBaseInfoMapper userBaseInfoMapper;
    @Autowired
    private UserCodeMapper userCodeMapper;

    @Override
    public User loadPhone(String mobile) {

        return userMapper.loadPhoneUser(mobile);
    }
    @GlobalTransactional(rollbackFor = Exception.class)
    @Override
    public void phoneRegister(UserDto userDto) {
       //校验空值
        if(StringUtils.isEmpty(userDto.getMobile()) || StringUtils.isEmpty(userDto.getPassword()) || StringUtils.isEmpty(userDto.getSmsCode()) ||
           StringUtils.isEmpty(userDto.getImageCode())){

            throw new BusinessException("信息不能为空");
        }
        //手机验证码是否正确,是否过期
        Object obj = redisTemplate.opsForValue().get("register:" + userDto.getMobile());
       if(obj==null){

           throw  new BusinessException("手机验证码过期");
       }
       if (!userDto.getSmsCode().equals(obj.toString().split(":")[0])){
           throw new BusinessException("手机验证码错误");
       }

         User user =  userDto2User(userDto);
        Login login  =  user2Login(user);
        //添加到login中
        JSONResult register = uaaFeignClient.register(login);
        //查找当前添加的这个login
        long l = Long.parseLong(register.getData().toString());
        //将当前这个login的id在user中设置LoginId
        user.setLoginId(l);
        //添加到user
        userMapper.insert(user);
        //设置userAccount表中
        UserAccount userAccount = new UserAccount();
        userAccount.setId(user.getId());
        userAccount.setCreateTime(new Date().getTime());
        userAccount.setUsableAmount(null);
        userAccount.setFrozenAmount(null);
         userAccountMapper.insert(userAccount);
         //添加到userBaseInfo表中
        UserBaseInfo userBaseInfo = new UserBaseInfo();
        userBaseInfo.setId(user.getId());
        userBaseInfo.setCreateTime(new Date().getTime());
        userBaseInfo.setRegChannel(1);
        userBaseInfoMapper.insert(userBaseInfo);
        //添加到user_code表中
        UserCode userCode = new UserCode();
        userCode.setCode(userDto.getCode());
        userCode.setUserId(12L);
        userCodeMapper.insert(userCode);



    }

    private Login user2Login(User user) {
        Login login = new Login();
        login.setUsername(user.getPhone());
        login.setType(1);
        login.setEnabled(true);
        return login;

    }

    private User userDto2User(UserDto userDto) {

        User user = new User();
        String mobile = userDto.getMobile();
        user.setPhone(mobile);
        user.setNickName(mobile);

        //设置状态
        long l = BitStatesUtils.addState(BitStatesUtils.OP_REGISTED, BitStatesUtils.OP_ACTIVED);
        long state = BitStatesUtils.addState(l, BitStatesUtils.OP_AUTHED_PHONE);
        user.setBitState(state);

        user.setCreateTime(new Date().getTime());

        return user;
    }


}
